<?php
/**
 * 会员等级管理
 */
defined('InShopNC') or exit('Access Invalid!');
class member_levelControl extends SystemControl{
    /**
     * @var member_levelModel
     */
    protected $_memberLevelModel;
    public function __construct(){
        parent::__construct();
        Language::read('member');
        $this->_memberLevelModel=Model('member_level');
    }

    /**
     * 会员等级列表
     * @return  void
     */
    public function indexOp(){
        Tpl::output('memberLevelList',$this->_memberLevelModel->getLevelList());
        Tpl::showpage('member_level.index');
    }

    /**
     * 检查等级名称
     * @return  void
     */
    public function ajaxOp(){
        $lists=$this->_memberLevelModel->getLevelList();
        foreach($lists as $item){
            if($item['level_name']==$_GET['level_name']){
                echo 'false';
                return ;
            }
        }
        echo 'true';
    }
    /**
     * 删除会员等级
     * @return  void
     */
    public function level_delOp(){
        $ids=array();
        if($_POST['del_id']&&is_array($_POST['del_id'])){
            $ids=$_POST['del_id'];
        }elseif($_GET['level_id']){
            $ids=array((int)$_GET['level_id']);
        }
        if($ids){
            $this->_memberLevelModel->delLevel($ids);
        }
        $this->indexOp();
    }

    /**
     * 新增
     * @return  void
     */
    public function level_addOp(){
        if (chksubmit()){
            $this->_memberLevelModel->addLevel(
                array_intersect_key($_POST,array(
                    'level_name'=>1,
                    'member_name'=>1,
                    'price_name'=>1,
                    'min_points'=>1,
                    'max_points'=>1,
                    'discount'=>1,
                    'barter_day'=>1,
                ))
            );
            $this->indexOp();
        }else{
            Tpl::showpage('member_level.edit');
        }
    }
    public function level_editOp(){
        if (chksubmit()){
            $this->_memberLevelModel->where(array('member_level_id'=>$_POST['member_level_id']))->update(
                array_intersect_key($_POST,array(
                    'level_name'=>1,
                    'member_name'=>1,
                    'price_name'=>1,
                    'min_points'=>1,
                    'max_points'=>1,
                    'discount'=>1,
                    'barter_day'=>1,
                ))
            );
            $this->_memberLevelModel->cacheLevel();
            $this->indexOp();
        }else {
            Tpl::output('level',$this->_memberLevelModel->getLevel($_GET['level_id']));
            Tpl::showpage('member_level.edit');
        }
    }
}